import sys
import parser
import symboltable
filename = sys.argv[1] 
ifile = open(filename,'r')
strfile = ifile.read()
instr=strfile.split('\n')
i=0
l1=['','\r','\n']
instr_str=[]
for x in instr:
    if not x.startswith('//'): 
        if  x not in l1:
            instr_str.append(x.strip('\r'))
            address = 16
            sym=symboltable.Constructor()
            for x in instr_str:
                if x.find('@') >=0 or x.find('(')>=0:
                    symbol=parser.symbol(x)
                    if not symboltable.contains(symbol,sym) and not symbol.isdigit():
                        sym=symboltable.addEntry(symbol,address,sym)
                        address=address+1

while parser.hasMoreCommands(i,instr_str):
    c_type=parser.commandType(instr_str[i]) 
    if c_type=='A':                                     
        str1=parser.symbol(instr_str[i])
        if str1.isdigit():              
            str1=bin(int(str1))[2:]                           
            address = str1.zfill(16)                                         
            print address                                                                        
        else:
            if  symboltable.contains(str1,sym):
                str2=bin(symboltable.GetAddress(str1,sym))[2:] 
                address1=str2.zfill(16)
                print address1
    if c_type=='C':                        
        c_str=parser.comp(instr_str[i])
        d_str=parser.dest(instr_str[i])
        j_str=parser.jump(instr_str[i])
        print '111'+c_str+d_str+j_str
    i=i+1
